#ifndef CUDPCMD_H
#define CUDPCMD_H

#include <QThread>
#include <QUdpSocket>
#include <QProcess>
#include <QTcpServer>
#include <QTimer>
#include <QDateTime>
#include <QMutex>

class CUdpCmd : public QThread
{
    Q_OBJECT
public:
    explicit CUdpCmd(QThread *parent = 0);

    static void* cntDelegate;

    void ready(int port);
    QByteArray readFile(QString fileName);
    QByteArray writeFile(QString fileName, QString data);
    void execCMD(QString command, QStringList arg);
    void syncFileSystem();
    void reGetSystemTime(int server_hour);
    void init_watch_dog();
    int find_head(char *buf,int size);
    QList<QByteArray> splitData(QByteArray data,QByteArray sp);

    void checkUpdateVersion();
    void check_update();

    void updateDownload(QString filename, int filesize);
    void updateDownloadWithId(int id, int version, int filesize);

    void checkRTSPmode(QString mode);
    void checkKNX();
signals:
    void s_cmd(QByteArray d);
    void s_mcu_read(QByteArray d);

public slots:
    void readDataSocket();
    void readSerialData();
    void mucReadDataSocket();
    void doReboot();
    void doTimeout();
    void doDogTimeout();
    void doRunStream();
    void doStopStream();
    void writeSerial(QByteArray d);
    void mucUpdate(QByteArray d);
    void doUpdateMessage(QByteArray d);

protected:
    void run();

private:
    QUdpSocket *udp,*_muc_udp;
    bool _muc_update;

    QProcess _pro;
    QDateTime _curDT;
    int _curHour;
    QTimer _timer,_timer_dog,_timer_stream;
    int _reboot_sec;
    int _watch_fd;

    QString _ip_mcu;
    int _port_mcu;

    QMutex _mutex;
};

class CTcpUpdateServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit CTcpUpdateServer(QObject *parent = 0);
    void ready(int port);

public slots:
    void doNewConnect();
private:

};

#endif // CUDPCMD_H
